/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AccountingPanel.java
 *
 * Created on 24-03-2011, 12:39:14
 */

package tiebreak.UI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import tiebreak.BE.MembershipType;
import tiebreak.BE.Settings;
import tiebreak.BL.Accounting;

/**
 *
 * @author Låner
 */
public class AccountingPanel extends javax.swing.JPanel {

    /** Creates new form AccountingPanel */
    public AccountingPanel() throws SQLException {
        initComponents();
        fillcbMemberType();
        setTxtField();

        MouseListener ml = new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e)
            {
                try {
                    UpdatePrice();
                } catch (SQLException ex) {
                    Logger.getLogger(AccountingPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };

        btnUpdate.addMouseListener(ml);

        ActionListener al = new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                try {
                    setMembershipTypeFee();
                } catch (SQLException ex) {
                    Logger.getLogger(AccountingPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        };

        cbMemberType.addActionListener(al);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbMemberType = new javax.swing.JComboBox();
        lblTS = new javax.swing.JLabel();
        txtForCB = new javax.swing.JTextField();
        btnUpdate = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        txtFieldSchool = new javax.swing.JTextField();
        txtFieldElite = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        lblTS.setText("Select type:");

        btnUpdate.setText("Update");

        jLabel1.setText("Type new price:");

        jLabel2.setText("Training School");

        jLabel3.setText("Elite Training");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(btnUpdate)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(lblTS, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(49, 49, 49)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 59, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbMemberType, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtFieldElite, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                            .addComponent(txtFieldSchool, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE)
                            .addComponent(txtForCB, javax.swing.GroupLayout.DEFAULT_SIZE, 161, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lblTS, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbMemberType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtForCB))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFieldSchool, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtFieldElite, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(24, 24, 24)
                .addComponent(btnUpdate)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cbMemberType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel lblTS;
    private javax.swing.JTextField txtFieldElite;
    private javax.swing.JTextField txtFieldSchool;
    private javax.swing.JTextField txtForCB;
    // End of variables declaration//GEN-END:variables

    private void fillcbMemberType() throws SQLException {
        DefaultComboBoxModel dcbm = new DefaultComboBoxModel();
            for(MembershipType type : Accounting.getMembershipTypes())
                dcbm.addElement(type.getName());
        cbMemberType.setModel(dcbm);
    }

    private void setTxtField() throws SQLException {
        for(Settings s : Accounting.getFees())
        {
            txtFieldSchool.setText(Integer.toString(s.getTennisSchoolPrice()));
            txtFieldElite.setText(Integer.toString(s.getElitePrice()));
        }
        setMembershipTypeFee();
    }

    private void UpdatePrice() throws SQLException {
        Accounting.setEliteAndSchool(Integer.parseInt(txtFieldSchool.getText()), Integer.parseInt(txtFieldElite.getText()));
        Accounting.setMemberTypeFee(cbMemberType.getSelectedItem().toString(), Integer.parseInt(txtForCB.getText()));
        JOptionPane.showMessageDialog(null, "The fee have been updated!");
    }

    private void setMembershipTypeFee() throws SQLException {
        for(MembershipType mt : Accounting.getMembershipTypeFee(cbMemberType.getSelectedItem().toString()))
            txtForCB.setText(Integer.toString(mt.getPrice()));
    }
}
